package sdp.group14.management.tenant;
import sdp.group14.R;
import sdp.group14.adapter.TenantEventsListAdapter;
import sdp.group14.appdata.Request;
import sdp.group14.management.HousingManagerApplication;
import sdp.group14.management.staff.StaffEventsListActivity;
import sdp.group14.management.staff.StaffViewRequestActivity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * this class is used to display the all the Requests stored in
 * the database
 * @author ste706
 *
 */
public class TenantRequestListActivity extends ListActivity{

	private HousingManagerApplication app;
	private TenantEventsListAdapter adapter;

	/**
	 * called when the class is first created
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tenant_request_list);

		try {
			app = (HousingManagerApplication) getApplication();
			adapter = new TenantEventsListAdapter(this, app.getCurrentEvents());
			setListAdapter(adapter);
		} catch (Exception e) {
			Log.e("requests", "setup adapter");
		}
	}
	
	
	public void onViewClick(final View view)
	{
		try {			
			Intent intent = new Intent(TenantRequestListActivity.this,
					TenantViewRequestActivity.class);
			TextView textView = (TextView) view;
			Request request = (Request) textView.getTag();
			intent.putExtra("request", request);
			startActivity(intent);
			Log.e("MAD", "pass request to request info activity");
		} catch (Exception e) {
			Log.e("MAD", "error occurs in passing request to request info activity");
		}
	}
	
	public void onDeleteClick(final View view)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.delete_comfirm)
				.setMessage(R.string.delete_message)
				.setPositiveButton("Delete",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								//remove the clicked Request in the list adapter
								//and also in the database
								try
								{
								ImageButton imgBtn = (ImageButton) view;
								Request r = (Request) imgBtn.getTag();
								//adapter.deleteRequest(r);
								//app.deleteRequest(r.getReqID());			
								}
								catch (Exception e)
								{
									Log.e("tenantRequestList", e.getMessage());
								}
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}

   /**
	 * if the delete button is clicked, ask for user's confirmation
	 * and delete all the Requests
	 * @param view
	 */
	public void onClearRequestsClick(View view) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.delete_comfirm)
				.setMessage(R.string.delete_all_message)
				.setPositiveButton("Delete",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								//remove all Requests in the list adapter
								//and also in the database								
								adapter.removeAllRequests();
								app.deleteAllRequests();
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		adapter.forceReload();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	    	try {			
				Intent intent = new Intent(TenantRequestListActivity.this,
						TenantManagementActivity.class);				
				startActivity(intent);
				
			} catch (Exception e) {
				Log.e("tenantViewRequestAct", "error occurs in backing to tenant entry activity");
			}
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}

	
}
